package com.telra.app.server;

import com.telra.app.server.cassandra.CassandraWriter;
import com.telra.app.server.esper.EsperService;
import com.telra.app.server.jms.Listener;
import com.telra.app.server.jms.Publisher;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Log4JLogger;


public class ServerFactory {

    private static final Log4JLogger log = (Log4JLogger) LogFactory.getFactory().getInstance(ServerFactory.class);
    public static enum SERVICES {
        ZK, LISTENER, PUBLISHER;
    }

    public static void main(String[] args) {

        // Add a shutdown hook
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run(){
                log.info ("closing down the shop...");
            }
        });

        if (args == null || args.length < 1) {
            log.error("Invalid start parameter + " + args);
            System.exit(-1);
        }



        String service = args[0];

        if (service.equals(SERVICES.ZK.toString())) {
            log.info("starting ZK service");
            new ZooKeeperService();
        }
        else if (service.equals(SERVICES.LISTENER.toString())) {
            CassandraWriter cs = new CassandraWriter();
            EsperService ep = new EsperService();
            new Listener(ep.getEPServiceProvider(), cs);	//bind services to listener
        }
        else if (service.equals(SERVICES.PUBLISHER.toString())) {
            new Publisher();
        }
        else {
            log.error("Invalid parameter = " + args[0]);
            System.exit(-2);
        }
    }
}
